/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kebut.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SoundPlayer {
    public static File music;
    public static InputStream in;
    public static AudioInputStream as;
    public static Clip clip;
    public static boolean isPause = false;
    public static long pausePosition = 0;
    
    public static void setMusic(File mus) {
        try {
            music = mus;
            as = null;
            in = new FileInputStream(music);
            InputStream bufferedIn = new BufferedInputStream(in);
            as = AudioSystem.getAudioInputStream(bufferedIn);
            clip = AudioSystem.getClip();
            // Open audio clip and load samples from the audio input stream.
            clip.open(as);
            isPause = false;
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(SoundPlayer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (LineUnavailableException ex) {
            Logger.getLogger(SoundPlayer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SoundPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void play() {
        if (!clip.isRunning()) {
            if (!isPause)
                clip.setFramePosition(0);
            else {
                clip.setMicrosecondPosition(pausePosition);
                isPause = false;
            }
            clip.start();
        }
    }
    
    public static void stop() {
        if (clip.isRunning())
            clip.stop();
        isPause = false;
    }
    
    public static void pause() {
        isPause = true;
        pausePosition = clip.getMicrosecondPosition();
        System.out.println("pause men" + isPause + "|" + pausePosition);
        clip.stop();
    }
}
